package com.lee.lease.common.sms;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.teaopenapi.models.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * ClassName: AliyunSMSPropertiesConfiguration
 * Package: com.lee.lease.common.sms
 * Description:
 */
@Configuration
@EnableConfigurationProperties(AliyunSMSProperties.class)
@ConditionalOnProperty(prefix = "aliyun.sms", name = "endpoint")
public class AliyunSMSPropertiesConfiguration {
    @Autowired
    private AliyunSMSProperties properties;

    @Bean
    public com.aliyun.dysmsapi20170525.Client client() throws Exception {
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考。
        // 建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html。
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
                config.setAccessKeyId(properties.getAccessKeyId());
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                config.setAccessKeySecret(properties.getAccessKeySecret());
                // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
                config.setEndpoint(properties.getEndpoint());
        return new com.aliyun.dysmsapi20170525.Client(config);
    }
}
